package com.atguigu.juc.completableFuture;

import java.util.concurrent.*;

/**
 * @author yfstart
 * @create 2022-06-13 13:48
 */
public class CompletableFutureBuildDemo {
  public static void main(String[] args) throws ExecutionException, InterruptedException {
    // runAsyncExtracted();
    supplyAsyncExtracted();
  }

  private static void supplyAsyncExtracted() throws InterruptedException, ExecutionException {
    ExecutorService threadPool = Executors.newFixedThreadPool(3);
    CompletableFuture<String> supplyAsync =
        CompletableFuture.supplyAsync(
            () -> {
              System.out.println(Thread.currentThread().getName() + "\t ---come in");
              try {
                TimeUnit.MILLISECONDS.sleep(500);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
              return "hello supplyAsync";
            },
            threadPool);
    System.out.println("supplyAsync.get() = " + supplyAsync.get());
    threadPool.shutdown();
  }

  private static void runAsyncExtracted() throws InterruptedException, ExecutionException {
    ExecutorService threadPool = Executors.newFixedThreadPool(3);
    CompletableFuture<Void> completableFuture =
        CompletableFuture.runAsync(
            () -> {
              System.out.println(Thread.currentThread().getName() + "\t ---come in");
              try {
                TimeUnit.MILLISECONDS.sleep(500);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
            },
            threadPool);
    System.out.println("completableFuture.get() = " + completableFuture.get());
    threadPool.shutdown();
  }
}
